/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snails;

import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.control.AbstractControl;
import shooter.Main;
import static shooter.WorldAppState.cannon;

/**
 *
 * @author ulfgur
 */
public class Cannon extends AbstractControl
{
    public static final int cannonRechargeMod = 5;
    
    private int damage;
    private double rechargeRate;
    //NOTE: full recharge is 100
    private double currentRecharge;
    private int volley;
    
    private Vector3f location;
    private Vector3f facing;
    
    private float fireRate;
    
    private CannonBay parent;
    
    
    public ParticleEmitter blast;
        
    
    /**
     * generates a standard cannon
     */
    public Cannon(CannonBay par)
    {
        parent = par;
        damage = 100;
        rechargeRate = 5;
        currentRecharge = 100;
        volley = 1;
        blast = Main.world.cannon.clone();
        blast.setParticlesPerSec(0);
        
        
    }
    
    public void initialize()
    {
        ((Node) spatial).attachChild(blast);
    }

    /**
     * fires cannon
     * @return cannon damage
     */
    public int fire()
    {
        int damageNow = (int) (damage * ((int) (volley * (currentRecharge / 100))));
        currentRecharge = 0;
        fireRate = 90;
        blast.setParticlesPerSec(90);
        blast.emitAllParticles();
        return damageNow; 
    }
    
    public int getDamage() {
        return damage;
    }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    public double getRechargeRate() {
        return rechargeRate;
    }

    public void setRechargeRate(double rechargeRate) {
        this.rechargeRate = rechargeRate;
    }

    public double getCurrentRecharge() {
        return currentRecharge;
    }

    public void setCurrentRecharge(double currentRecharge) {
        this.currentRecharge = currentRecharge;
    }

    public int getVolley() {
        return volley;
    }

    public void setVolley(int volley) {
        this.volley = volley;
    }

    public Vector3f getFacing() {
        return facing;
    }

    public void setFacing(Vector3f facing) {
        this.facing = facing;
        blast.getParticleInfluencer().setInitialVelocity(facing.normalize().mult(140));
    }

    public Vector3f getLocation() {
        return location;
    }

    public void setLocation(Vector3f location) {
        this.location = location;
        blast.setLocalTranslation(location);
    }

    public void setLocation(float x, float y, float z) {
        setLocation(new Vector3f(x, y, z));
    }
    
    
    
    
    
    
    
    @Override
    protected void controlUpdate(float tpf)
    {
        if(currentRecharge < 100)
        {
            currentRecharge += rechargeRate * tpf * cannonRechargeMod;
        }
        
        
        //Make the particles work
        if(fireRate > 0)
        {
            //fireRate affects the number of particles per second,...
            fireRate -= tpf * 80;
            if(fireRate > 0)
            {
                blast.setParticlesPerSec(fireRate);
            }else
            {
                //and basically resets the particle
                //emitter so that the next attack
                //renders properly.
                blast.killAllParticles();
            }
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {}
    
    
}
